<head><meta name="viewport" content="user-scalable=no,initial-scale=1,maximum-scale=1">
<meta charset=utf-8>
<style>
body { margin: 0px; }
canvas { width:100%; height:100%; overflow: hidden; }
</style>
<script type="text/javascript" src="../h3du_min.js"></script>
<script type="text/javascript" src="../extras/frame.js"></script>
<script type="text/javascript" src="../extras/curvetube.js"></script>
<script type="text/javascript" src="../extras/path.js"></script>
<script type="text/javascript" src="../extras/pathshapes.js"></script>
<script type="text/javascript" src="pathutil.js"></script>
<script type="text/javascript" src="demoutil.js"></script>
<script type="text/javascript" src="../extras/meshjson.js"></script>
<script src="https://peteroupc.github.io/colorpicker/cbox.js"></script>
</head>
<body>
<canvas id=canvas></canvas>
<script id="demo">
/* global H3DU, makeTubeFromPath, pathFloor */
// <!--
/*
 Any copyright to this file is released to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/
 If you like this, you should donate
 to Peter O. (original author of
 the Public Domain HTML 3D Library) at:
 http://peteroupc.github.io/
*/

function normalDist(lastNorm) {
 var ret
  if(!isNaN(lastNorm)) {
    ret=lastNorm[0]
    lastNorm[0]=Number.NaN
  } else {
    var x,y,s,t
    do { x=Math.random() } while(x==0);
    y=Math.random();
    s=Math.sqrt(-2*Math.log(x))
    t=Math.PI*2*y
    var ss=Math.sin(t)
    var cc=Math.cos(t)
    lastNorm[0]=s*ss
    ret=s*cc
  }
  return ret
}

function segsIntersect(a1,a2,b1,b2) {
 var c1 = (a1[0] - b2[0]) * (a2[1] - b2[1]) - (a1[1] - b2[1]) * (a2[0] - b2[0]);
 var c2 = (a1[0] - b1[0]) * (a2[1] - b1[1]) - (a1[1] - b1[1]) * (a2[0] - b1[0]);
  if (!(Math.abs(c1) < 1e-9) && !(Math.abs(c2) < 1e-9) && c1 * c2 < 0) {
   var c3= (b1[0] - a1[0]) * (b2[1] - a1[1]) - (b1[1] - a1[1]) * (b2[0] - a1[0]);
    var c4 = c3 + c2 - c1;
    return (c3 * c4 < 0);
   }
  return false
}
// Inspired by http://stackoverflow.com/questions/8997099
function randomPolygon(cx,cy,avgRadius,sides,irregular,spiky) {
  var irregularValue=((typeof irregular==="undefined" || irregular===null) ? (0) : (irregular))
  var spikyValue=((typeof spiky==="undefined" || spiky===null) ? (0) : (spiky))
  var step=H3DU.Math.PiTimes2/sides
  var avg2=avgRadius*2
  var failed=false
  do {
  var theta=Math.random()*H3DU.Math.PiTimes2
  var deltas=[]
  var points=[]
  var k=0
  var lastNorm=[Number.NaN]
  for(var i=0;i<sides;i++) {
    var d=(step-irregularValue)+Math.random()*
       ((step+irregularValue)-(step-irregularValue))
    k+=d/H3DU.Math.PiTimes2
    deltas.push(d)
  }
  if(k==0)k=0.01
  for(var i=0;i<sides;i++) {
    var r=normalDist(lastNorm)*spikyValue+avgRadius
    if(r<0)r=(avg2/100.0)
    if(r>avg2)r=avg2
    var s=Math.sin(theta)
    var c=Math.cos(theta)
    points.push([c*r+cx,s*r+cy])
    theta+=deltas[i]/k
  }
  failed=false
  if(points.length>2) {
  // Check for self-intersections
   var selfint=false
   for(var j=0;!selfint && j<points.length;j++) {
    var a1=points[j]
    var a2=(j==points.length-1) ? points[0] : points[j+1]
    for(var k=j+1;!selfint && k<points.length;k++) {
     var b1=points[k]
     var b2=(k==points.length-1) ? points[0] : points[k+1]
     selfint|=segsIntersect(a1,a2,b1,b2)
    }
   }
   failed|=selfint
  }
  } while(failed)
  return points
}
var path=new GraphicsPath()
var poly=randomPolygon(0,0,20,15,8,4)
for(var i=0;i<poly.length;i++) {
 if(i==0)path.moveTo(poly[i][0],poly[i][1])
 else path.lineTo(poly[i][0],poly[i][1])
}
path.closePath()

var scene = new H3DU.Scene3D(document.getElementById("canvas"));
scene.setClearColor("white");
var sub = new H3DU.Batch3D()
   .perspectiveAspect(45, 1, 1000)
   .setLookAt([0,0, 1.2 * 50], [0,0, 0], [0, 1,0]);
var fc = new H3DU.FrameCounterDiv();
var group = new H3DU.ShapeGroup();
sub.getLights()
    .setDirectionalLight(0, [0, -3, 1.2], [1, 1, 1])
    .setDirectionalLight(1, [0, 3, 1.2], [0.6, 0.6, 0.6]);
var mesh = null;
mesh = makeTubeFromPath(path, 0.05, 0.5);
sub.addShape(
    new H3DU.Shape(mesh).setColor("blue"));
sub.addShape(
    new H3DU.Shape(pathFloor(path)).setColor("#eeeeee"));
sub.addShape(group);
H3DU.renderLoop(function() {
  "use strict";
  fc.update();
  scene.render(sub);
});
// -->
</script>
</body>
